package pl.puntin.punclient.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import pl.puntin.punclient.controller.Controller;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Luk
 */
public class RoomView extends JPanel {

	private javax.swing.JButton CreateRoomButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton joinRoomButton;
    private javax.swing.JButton logoutButton;
    private javax.swing.JButton refreshListButton;
    private javax.swing.JList roomList;
    private final Controller controler;

    private String[] roomsIdList;
    /**
     * Creates new form roomView
     */
    public RoomView(final Controller controler) {
        this.controler = controler;
        setPreferredSize(new Dimension(600, 400));
    	initComponents();        
        
    }


    private void initComponents() {

        refreshListButton = new javax.swing.JButton();
        CreateRoomButton = new javax.swing.JButton();
        joinRoomButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        roomList = new javax.swing.JList();
        logoutButton = new javax.swing.JButton();

        refreshListButton.setText("Odśwież listę pokoi");
        refreshListButton.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(final ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }

			private void refreshButtonActionPerformed(final ActionEvent evt) {
				controler.getRoomList();				
			}
        });
        
        CreateRoomButton.setText("Utwórz nowy pokój");
        CreateRoomButton.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(final ActionEvent evt) {
                CreateRoomButtonActionPerformed(evt);
            }

			private void CreateRoomButtonActionPerformed(final ActionEvent evt) {
				controler.creatRoom();				
			}
        });

        joinRoomButton.setText("Dołącz do pokoju");
        joinRoomButton.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(final ActionEvent evt) {
                joinRoomButtonActionPerformed(evt);
            }

			private void joinRoomButtonActionPerformed(final ActionEvent evt) {
				int i = roomList.getSelectedIndex();
				if(i == -1)
					controler.popMessage("Aby dołączyć do gry musisz wybrać pokój.");
				else
					controler.joinRoom(roomsIdList[i]);				
			}
        });

        logoutButton.setText("Wyjdz z gry");
        logoutButton.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(final ActionEvent evt) {
            	logoutButtonActionPerformed(evt);
            }

			private void logoutButtonActionPerformed(final ActionEvent evt) {
				controler.logout();				
			}
        });
        
        roomList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        roomList.setToolTipText("");
        roomList.setOpaque(false);
        jScrollPane1.setViewportView(roomList);


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(refreshListButton, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                    .addComponent(joinRoomButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(CreateRoomButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(logoutButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(refreshListButton)
                        .addGap(73, 73, 73)
                        .addComponent(CreateRoomButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(joinRoomButton)
                        .addGap(78, 78, 78)
                        .addComponent(logoutButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        setVisible(true);
    }
    /**
     * Ustawia elementy w JLi�cie
     */
    public void setList(final String[] roomId, final String[] roomPlayers)
    {
    	roomsIdList = roomId;
    	String[] displayList = new String[roomId.length];
    	for(int i =0; i<roomId.length; i++)
    		displayList[i]= "Room ID: "+ roomId[i]+" Players: "+roomPlayers[i];
    	roomList.clearSelection();
    	roomList.setListData(displayList);
    	roomList.repaint();
    }
    
}

